package com.itany.netclass.service.proxy;

import com.itany.netclass.entity.User;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.FrontMainControllerService;
import com.itany.netclass.tran.TransactionManager;

/**
 * Author:sjy@itany.com
 * Date:21-10-20 上午11:34
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class FrontMainControllerServiceProxy implements FrontMainControllerService {
    @Override
    public User login(String username, String password) throws ServiceException, NameOrPasswordException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        FrontMainControllerService frontMainControllerService  = (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerServiceTarget");
        try {
            tran.begin();
            User login = frontMainControllerService.login(username, password);
            tran.commit();
            return login;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NameOrPasswordException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaUsername(String username) throws ServiceException, NullSringException, EmailExistException, TpyeException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        FrontMainControllerService frontMainControllerService  = (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerServiceTarget");
        try {
            tran.begin();
            frontMainControllerService.jianchaUsername(username);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (EmailExistException e) {
            tran.rollback();
            throw e;
        } catch (TpyeException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaPassword(String password) throws ServiceException, NullSringException, TpyeException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        FrontMainControllerService frontMainControllerService  = (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerServiceTarget");
        try {
            tran.begin();
            frontMainControllerService.jianchaPassword(password);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (TpyeException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public void jiancharePassword(String password,String rePassword) throws ServiceException, NullSringException, TpyeException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        FrontMainControllerService frontMainControllerService  = (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerServiceTarget");
        try {
            tran.begin();
            frontMainControllerService.jiancharePassword(password, rePassword);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (TpyeException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jiachaNickname(String nickname) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        FrontMainControllerService frontMainControllerService  = (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerServiceTarget");
        try {
            tran.begin();
            frontMainControllerService.jiachaNickname(nickname);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaEmil(String email) throws TpyeException, NullSringException, EmailExistException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        FrontMainControllerService frontMainControllerService  = (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerServiceTarget");
        try {
            tran.begin();
            frontMainControllerService.jianchaEmil(email);
            tran.commit();
        } catch (TpyeException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (EmailExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void addUser(String username, String password, String nickname, String email) throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        FrontMainControllerService frontMainControllerService  = (FrontMainControllerService) ObjectFactory.getObject("FrontMainControllerServiceTarget");
        try {
            tran.begin();
            frontMainControllerService.addUser(username,password,nickname,email);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }
}
